package com.sasken.pdb.controllers
{
	import com.sasken.pdb.controllers.remoting.DatalRemoting;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.core.Application;
	import mx.core.IFlexDisplayObject;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	/**
	 *  This class is a base controller for all other controllers. 
	 *  It contains the basic functionality required from controller objects.
	 * 
	 * <p>Basic functionality is providing an interface to get and set views for controller classes.
	 * They are protected to enforce the behavior that only the controller responsible for 
	 * a view could access the view. Other controllers access other views than their own through the 
	 * interface provided by a controller.</p>
	 * <p></p>
	 */
	public class BaseController implements IEventDispatcher
	{
		private var _view:UIComponent;
		private var _dataRemoting:DataRemoting;
		
		private var dispatcher:EventDispatcher;
		
		/**
		 *  Constructor, initializes this object.
		 * 
		 * @param The view being controlled by this controller.
		*/
		public function BaseController(controlledView:UIComponent)
		{
			dispatcher = new EventDispatcher(this);
			_dataRemoting = DataRemoting.instance;
			view = controlledView;
			
		}
		
		/**
		 * Sets the view being controlled.
		 * 
		 * @param _controlledView The view associated with this controller.
		*/
		protected function set view(_controlledView:UIComponent):void
		{
			_view = _controlledView;
		}
		
		/**
		 * Returns the DataModelRemoting instance used for getting data providers
		 * for view elements. Although DataModelRemoting can be accessed directly, usage
		 * of this controller-internal function is highly recommended for clarity.
		 * @return The DataModelRemoting instance.
		*/
		protected final function get dataRemoting():DataRemoting
		{
			return _dataRemoting;
		}	
		
		/**
		 * Returns the reference to the view being controlled.
		 * 
		 * @return The UIComponent reference to the view being controlled.
		*/
		protected function get view():UIComponent
		{
			return _view;
		}
		
		/**
		 * Opens a window instance through a PopupManager so that it can be controlled later.
		 * The popup is centered on the screen and it is modal. A subclass can override 
		 * this behavior if necessary.
		 * @param window The window to be opened through PopupManager.
		*/	
		protected function openWindow(window:IFlexDisplayObject):void
		{
			// Application parent makes the window centered related to app (whole screen) 
			PopUpManager.addPopUp(window, Application.application as DisplayObject, true);
			PopUpManager.centerPopUp(window);			
		}
		
		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
	        dispatcher.addEventListener(type, listener, useCapture, priority);
	    }
	           
	    public function dispatchEvent(evt:Event):Boolean{
	        return dispatcher.dispatchEvent(evt);
	    }
	    
	    public function hasEventListener(type:String):Boolean
	    {
	        return dispatcher.hasEventListener(type);
	    }
	    
	    public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
	        dispatcher.removeEventListener(type, listener, useCapture);
	    }
	                   
	    public function willTrigger(type:String):Boolean {
	        return dispatcher.willTrigger(type);
	    }

	}
}